package com.myKng.shareservice.controller;

import com.myKng.shareservice.dto.ShareLinkRequest;
import com.myKng.shareservice.dto.ShareLinkResponse;
import com.myKng.shareservice.service.ShareService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分享控制器
 */
@RestController
@RequestMapping("/api/share")
@RequiredArgsConstructor
public class ShareController {

    private final ShareService shareService;

    /**
     * 创建分享链接
     *
     * @param request   分享链接请求
     * @param creatorId 创建者ID
     * @return 分享链接响应
     */
    @PostMapping
    public ResponseEntity<ShareLinkResponse> createShareLink(
            @RequestBody @Valid ShareLinkRequest request,
            @RequestHeader("X-User-ID") Long creatorId) {
        ShareLinkResponse response = shareService.createShareLink(request, creatorId);
        return new ResponseEntity<>(response, HttpStatus.CREATED);
    }

    /**
     * 获取分享链接详情
     *
     * @param shareCode 分享码
     * @return 分享链接响应
     */
    @GetMapping("/{shareCode}")
    public ResponseEntity<ShareLinkResponse> getShareLink(@PathVariable String shareCode) {
        ShareLinkResponse response = shareService.getShareLinkByCode(shareCode);
        return ResponseEntity.ok(response);
    }

    /**
     * 验证分享链接访问权限
     *
     * @param shareCode 分享码
     * @param password  访问密码
     * @param userId    用户ID
     * @return 是否有权限访问
     */
    @GetMapping("/{shareCode}/verify")
    public ResponseEntity<Boolean> verifyShareLinkAccess(
            @PathVariable String shareCode,
            @RequestParam(required = false) String password,
            @RequestHeader(value = "X-User-ID", required = false) Long userId) {
        boolean hasAccess = shareService.verifyShareLinkAccess(shareCode, password, userId);
        return ResponseEntity.ok(hasAccess);
    }

    /**
     * 记录分享链接访问
     *
     * @param shareCode 分享码
     * @return 成功响应
     */
    @PostMapping("/{shareCode}/access")
    public ResponseEntity<Void> recordShareLinkAccess(@PathVariable String shareCode) {
        shareService.recordShareLinkAccess(shareCode);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取用户创建的所有分享链接
     *
     * @param creatorId 创建者ID
     * @return 分享链接响应列表
     */
    @GetMapping("/user")
    public ResponseEntity<List<ShareLinkResponse>> getUserShareLinks(
            @RequestHeader("X-User-ID") Long creatorId) {
        List<ShareLinkResponse> responses = shareService.getUserShareLinks(creatorId);
        return ResponseEntity.ok(responses);
    }

    /**
     * 禁用分享链接
     *
     * @param shareCode 分享码
     * @param creatorId 创建者ID
     * @return 成功响应
     */
    @DeleteMapping("/{shareCode}")
    public ResponseEntity<Void> disableShareLink(
            @PathVariable String shareCode,
            @RequestHeader("X-User-ID") Long creatorId) {
        shareService.disableShareLink(shareCode, creatorId);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新分享链接
     *
     * @param shareCode 分享码
     * @param request   分享链接请求
     * @param creatorId 创建者ID
     * @return 更新后的分享链接响应
     */
    @PutMapping("/{shareCode}")
    public ResponseEntity<ShareLinkResponse> updateShareLink(
            @PathVariable String shareCode,
            @RequestBody @Valid ShareLinkRequest request,
            @RequestHeader("X-User-ID") Long creatorId) {
        ShareLinkResponse response = shareService.updateShareLink(shareCode, request, creatorId);
        return ResponseEntity.ok(response);
    }
}